package com.moto.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moto.entity.CarBrand;
import com.moto.entityDto.CarBrandDto;
import com.moto.entityDto.CarBrandLikeDto;
import com.moto.interceptor.BaseContext;
import com.moto.result.Result;
import com.moto.service.ICarBrandService;
import com.moto.utils.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 车辆表 前端控制器
 * </p>
 *
 * @author hyf
 * @since 2023-08-28
 */
@RestController
@RequestMapping("/admin/carBrand")
@Slf4j
@CrossOrigin(origins = "*")
@Api(tags = "摩托品牌相关接口")
public class CarBrandController {
    @Autowired
    private ICarBrandService carBrandService;
    /**
     * 新增品牌
     */
    @PostMapping("/addCarBrand")
    @ApiOperation("新增摩托车品牌")
    public Result<CarBrand> addCar(@RequestBody CarBrand carBrand){
        System.out.println("时间为："+ LocalDateTime.now());
        log.info("新增品牌：{}",carBrand);
        carBrand.setCreate_time(LocalDateTime.now());
        carBrand.setIsDelete(1);
        carBrandService.save(carBrand);
        return Result.success();
    }


    /**
     * 用户端查询摩托品牌并按照ASCII值进行排序
     */
    @GetMapping("/AsciiFindAllCarBrand")
    @ApiOperation("摩托品牌并按照ASCII值进行排序")
    public Result<List<CarBrand>> AsciiFindAllCarBrand(){
        QueryWrapper<CarBrand> wrapper = new QueryWrapper<>();
        wrapper.orderBy(true,true,"init");
        List<CarBrand> list = carBrandService.list(wrapper);
        return Result.success(list);
    }


    /**
     * 修改摩托车品牌信息
     * @param carBrandDTO
     * @return
     */
    @PutMapping("/updateCarBrand")
    @ApiOperation("修改摩托车品牌信息")
    public Result<CarBrandDto> updateCarBrand(@RequestBody CarBrandDto carBrandDTO){
        Integer id = Math.toIntExact(BaseContext.getCurrentId());
        carBrandDTO.setUpdateId(id);
        carBrandDTO.setUpdateTime(LocalDateTime.now());
        log.info("修改摩托车的信息为：{}"+carBrandDTO);
        carBrandService.updateCarBrand(carBrandDTO);
        return Result.success();
    }

    /**
     * 分页模糊查询车辆品牌
     * @param carBrandLikeDto
     * @return
     */
    @GetMapping("/likeCarBrand")
    @ApiOperation("模糊查询车辆品牌")
    public Result<PageResult> likeCarBrand(CarBrandLikeDto carBrandLikeDto){
        log.info("模糊查询车辆品牌：{}",carBrandLikeDto);
        PageResult pageResult  = carBrandService.likeCarBrand(carBrandLikeDto);
        if (pageResult.getTotal()==0){
            return Result.error("没有查找到你所要查找的品牌");
        }
        return Result.success(pageResult);
    }


    /**
     * 摩托车品牌的假删除
     * @param id
     * @return
     */
    @DeleteMapping("/deleteCar/{id}")
    @ApiOperation("摩托车假删除")
    public Result carBrandDelete(@PathVariable int id){
        carBrandService.removeById(id);
        return Result.success();
    }
}
